# Definition for a binary tree node.
import collections
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        queue = collections.deque()
        a = []
        queue.append((root,0))


        while len(queue) > 0 :
            node,level = queue.popleft()
            if node:
                if len(a) < level +1:
                    a.insert(0,[])
                a[-(level+1)].append(node.val)
                queue.append((node.left,level+1))
                queue.append((node.right, level + 1))

        return a



m1 = TreeNode(1)
m1.left = TreeNode(2)
m1.right = TreeNode(3)
m1.right.right = TreeNode(3)
# m1.right.right.right = TreeNode(3)
# m1.right.right.right.right= TreeNode(3)

s = Solution()

t = s.levelOrderBottom(m1)
print(t)
